﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F4CheckRR732 : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F4CheckRR732()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.RR732.DoubleClick += RR732_DoubleClick;
            this.RR732.PopupMenuShowing += RR732_PopupMenuShowing;
            this.f4RR732ContextMenu.ItemClicked += f4RR732ContextMenu_ItemClicked;
        }

        /////////////////////////////
        /// Call Edit Form
        /////////////////////////////
        private void callEditForm()
        {
            DataRow row = RR732.GetFocusedDataRow();
            frm_Edit_F4CheckRR732 editForm = new frm_Edit_F4CheckRR732();
            editForm.serial = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                //LoadData();
            }
            else
            {
                MessageBox.Show("Cannot edit selected row.");
            }
        }

        void f4RR732ContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f4RR732ContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F4CheckRR732 add_Form = new frm_Add_F4CheckRR732();
                    add_Form.ShowDialog();
                    //LoadData();
                    break;                
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void RR732_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f4RR732ContextMenu.Show(MousePosition);
        }

        private void RR732_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();                        
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT * FROM [231_UnitRR732]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridRR732.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("SerialNo", typeof(string));
                newTable.Columns.Add("Written", typeof(string));
                newTable.Columns.Add("Date", typeof(string));
                newTable.Columns.Add("Checked", typeof(string));
                newTable.Columns.Add("Wiring Daiagram", typeof(string));
                newTable.Columns.Add("PC software ver", typeof(string));
                newTable.Columns.Add("PC IP", typeof(string));
                newTable.Columns.Add("Host name", typeof(string));
                newTable.Columns.Add("RC400 ver", typeof(string));
                newTable.Columns.Add("RS485 ver", typeof(string));
                newTable.Columns.Add("PC Power", typeof(string));
                newTable.Columns.Add("PC Power Serial", typeof(string));
                newTable.Columns.Add("Upper RC420", typeof(string));
                newTable.Columns.Add("Upper RC420 Ver", typeof(string));
                newTable.Columns.Add("Upper RC420 Ex ver", typeof(string));
                newTable.Columns.Add("Upper RC420 No", typeof(string));
                newTable.Columns.Add("Upper RD026", typeof(string));
                newTable.Columns.Add("Upper run", typeof(string));
                newTable.Columns.Add("Upper stop", typeof(string));
                newTable.Columns.Add("Lower RC420", typeof(string));
                newTable.Columns.Add("Lower RC420 Ver", typeof(string));
                newTable.Columns.Add("Lower RC420 Ex ver", typeof(string));
                newTable.Columns.Add("Lower RC420 No", typeof(string));
                newTable.Columns.Add("Lower RD026", typeof(string));
                newTable.Columns.Add("Lower run", typeof(string));
                newTable.Columns.Add("Lower stop", typeof(string));
                newTable.Columns.Add("A RC420", typeof(string));
                newTable.Columns.Add("A RC420 Ver", typeof(string));
                newTable.Columns.Add("A RC420 Ex ver", typeof(string));
                newTable.Columns.Add("A RC420 No", typeof(string));
                newTable.Columns.Add("A RD026", typeof(string));
                newTable.Columns.Add("A run", typeof(string));
                newTable.Columns.Add("A stop", typeof(string));
                newTable.Columns.Add("Bar RC420", typeof(string));
                newTable.Columns.Add("Bar RC420 Ver", typeof(string));
                newTable.Columns.Add("Bar RC420 Ex ver", typeof(string));
                newTable.Columns.Add("Bar RC420 No", typeof(string));
                newTable.Columns.Add("Bar RD026", typeof(string));
                newTable.Columns.Add("Bar run", typeof(string));
                newTable.Columns.Add("Bar stop", typeof(string));
                newTable.Columns.Add("Z RC420", typeof(string));
                newTable.Columns.Add("Z RC420 Ver", typeof(string));
                newTable.Columns.Add("Z RC420 Ex ver", typeof(string));
                newTable.Columns.Add("Z RC420 No", typeof(string));
                newTable.Columns.Add("Z RD026", typeof(string));
                newTable.Columns.Add("Z run", typeof(string));
                newTable.Columns.Add("Z stop", typeof(string));
                newTable.Columns.Add("RC431", typeof(string));
                newTable.Columns.Add("RC431 ROM ver", typeof(string));
                newTable.Columns.Add("RC431 ver", typeof(string));
                newTable.Columns.Add("RC431 no", typeof(string));
                newTable.Columns.Add("Upper-OH", typeof(string));
                newTable.Columns.Add("Upper-OL", typeof(string));
                newTable.Columns.Add("Upper-PE", typeof(string));
                newTable.Columns.Add("Upper-OA", typeof(string));
                newTable.Columns.Add("Upper-OB", typeof(string));
                newTable.Columns.Add("Upper-OS", typeof(string));
                newTable.Columns.Add("Lower-OH", typeof(string));
                newTable.Columns.Add("Lower-OL", typeof(string));
                newTable.Columns.Add("Lower-PE", typeof(string));
                newTable.Columns.Add("Lower-OA", typeof(string));
                newTable.Columns.Add("Lower-OB", typeof(string));
                newTable.Columns.Add("Lower-OS", typeof(string));
                newTable.Columns.Add("A-OH", typeof(string));
                newTable.Columns.Add("A-OL", typeof(string));
                newTable.Columns.Add("A-PE", typeof(string));
                newTable.Columns.Add("A-OA", typeof(string));
                newTable.Columns.Add("A-OB", typeof(string));
                newTable.Columns.Add("A-OS", typeof(string));
                newTable.Columns.Add("B-OH", typeof(string));
                newTable.Columns.Add("B-OL", typeof(string));
                newTable.Columns.Add("B-PE", typeof(string));
                newTable.Columns.Add("B-OA", typeof(string));
                newTable.Columns.Add("B-OB", typeof(string));
                newTable.Columns.Add("B-OS", typeof(string));
                newTable.Columns.Add("Z-OH", typeof(string));
                newTable.Columns.Add("Z-OL", typeof(string));
                newTable.Columns.Add("Z-PE", typeof(string));
                newTable.Columns.Add("Z-OA", typeof(string));
                newTable.Columns.Add("Z-OB", typeof(string));
                newTable.Columns.Add("Z-OS", typeof(string));
                newTable.Columns.Add("Z-0S", typeof(string));
                newTable.Columns.Add("Inter-OH", typeof(string));
                newTable.Columns.Add("Inter-OL", typeof(string));
                newTable.Columns.Add("Inter-OA", typeof(string));
                newTable.Columns.Add("Inter-OB", typeof(string));
                newTable.Columns.Add("Inter-OS", typeof(string));
                newTable.Columns.Add("Remark", typeof(string));

                gridRR732.DataSource = newTable;
            }
            RR732.Columns[0].Caption = "SerialNo";
            RR732.Columns[1].Caption = "Written";
            RR732.Columns[2].Caption = "Date";
            RR732.Columns[3].Caption = "Checked";
            RR732.Columns[4].Caption = "Wiring Daiagram";
            RR732.Columns[5].Caption = "PC software ver";
            RR732.Columns[6].Caption = "PC IP";
            RR732.Columns[7].Caption = "Host name";
            RR732.Columns[8].Caption = "RC400 ver";
            RR732.Columns[9].Caption = "RS485 ver";
            RR732.Columns[10].Caption = "PC Power";
            RR732.Columns[11].Caption = "PC Power Serial";
            RR732.Columns[12].Caption = "Upper RC420";
            RR732.Columns[13].Caption = "Upper RC420 Ver";
            RR732.Columns[14].Caption = "Upper RC420 Ex ver";
            RR732.Columns[15].Caption = "Upper RC420 No";
            RR732.Columns[16].Caption = "Upper RD026";
            RR732.Columns[17].Caption = "Upper run";
            RR732.Columns[18].Caption = "Upper stop";
            RR732.Columns[19].Caption = "Lower RC420";
            RR732.Columns[20].Caption = "Lower RC420 Ver";
            RR732.Columns[21].Caption = "Lower RC420 Ex ver";
            RR732.Columns[22].Caption = "Lower RC420 No";
            RR732.Columns[23].Caption = "Lower RD026";
            RR732.Columns[24].Caption = "Lower run";
            RR732.Columns[25].Caption = "Lower stop";
            RR732.Columns[26].Caption = "A RC420";
            RR732.Columns[27].Caption = "A RC420 Ver";
            RR732.Columns[28].Caption = "A RC420 Ex ver";
            RR732.Columns[29].Caption = "A RC420 No";
            RR732.Columns[30].Caption = "A RD026";
            RR732.Columns[31].Caption = "A run";
            RR732.Columns[32].Caption = "A stop";
            RR732.Columns[33].Caption = "Bar RC420";
            RR732.Columns[34].Caption = "Bar RC420 Ver";
            RR732.Columns[35].Caption = "Bar RC420 Ex ver";
            RR732.Columns[36].Caption = "Bar RC420 No";
            RR732.Columns[37].Caption = "Bar RD026";
            RR732.Columns[38].Caption = "Bar run";
            RR732.Columns[39].Caption = "Bar stop";
            RR732.Columns[40].Caption = "Z RC420";
            RR732.Columns[41].Caption = "Z RC420 Ver";
            RR732.Columns[42].Caption = "Z RC420 Ex ver";
            RR732.Columns[43].Caption = "Z RC420 No";
            RR732.Columns[44].Caption = "Z RD026";
            RR732.Columns[45].Caption = "Z run";
            RR732.Columns[46].Caption = "Z stop";
            RR732.Columns[47].Caption = "RC431";
            RR732.Columns[48].Caption = "RC431 ROM ver";
            RR732.Columns[49].Caption = "RC431 ver";
            RR732.Columns[50].Caption = "RC431 no";
            RR732.Columns[51].Caption = "Upper-OH";
            RR732.Columns[52].Caption = "Upper-OL";
            RR732.Columns[53].Caption = "Upper-PE";
            RR732.Columns[54].Caption = "Upper-OA";
            RR732.Columns[55].Caption = "Upper-OB";
            RR732.Columns[56].Caption = "Upper-OS";
            RR732.Columns[57].Caption = "Lower-OH";
            RR732.Columns[58].Caption = "Lower-OL";
            RR732.Columns[59].Caption = "Lower-PE";
            RR732.Columns[60].Caption = "Lower-OA";
            RR732.Columns[61].Caption = "Lower-OB";
            RR732.Columns[62].Caption = "Lower-OS";
            RR732.Columns[63].Caption = "A-OH";
            RR732.Columns[64].Caption = "A-OL";
            RR732.Columns[65].Caption = "A-PE";
            RR732.Columns[66].Caption = "A-OA";
            RR732.Columns[67].Caption = "A-OB";
            RR732.Columns[68].Caption = "A-OS";
            RR732.Columns[69].Caption = "B-OH";
            RR732.Columns[70].Caption = "B-OL";
            RR732.Columns[71].Caption = "B-PE";
            RR732.Columns[72].Caption = "B-OA";
            RR732.Columns[73].Caption = "B-OB";
            RR732.Columns[74].Caption = "B-OS";
            RR732.Columns[75].Caption = "Z-OH";
            RR732.Columns[76].Caption = "Z-OL";
            RR732.Columns[77].Caption = "Z-PE";
            RR732.Columns[78].Caption = "Z-OA";
            RR732.Columns[79].Caption = "Z-OB";
            RR732.Columns[80].Caption = "Z-OS";
            RR732.Columns[81].Caption = "Z-0S";
            RR732.Columns[82].Caption = "Inter-OH";
            RR732.Columns[83].Caption = "Inter-OL";
            RR732.Columns[84].Caption = "Inter-OA";
            RR732.Columns[85].Caption = "Inter-OB";
            RR732.Columns[86].Caption = "Inter-OS";
            RR732.Columns[87].Caption = "Remark";

            RR732.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_F4CheckRR732 add_Form = new frm_Add_F4CheckRR732();
            add_Form.ShowDialog();
            //LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
